package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.organization.util.BusinessDistrict;
import com.turing.post.organization.util.BusinessDistrictDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 18, 2012
 */
@Stateless(mappedName = "organization.impl.BusinessDistrictDaoImpl")
@Remote
@Local
public class BusinessDistrictDaoImpl implements BusinessDistrictDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BusinessDistrict getBusinessDistrictByBusinessDistrictId(
			BigInteger businessDistrictId) {
		return _em.find(BusinessDistrict.class, businessDistrictId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrict> getBusinessDistrictList(
			PCPSWebInput webInput, Integer page) {
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select * from BusinessDistrict t where t.IssueCompanyNo = '"
					+ issueCompanyNo
					+ "' order by t.SuperBusinessDistrictId,t.BusinessDistrictName";
		} else {
			sql = "select * from BusinessDistrict t order by t.SuperBusinessDistrictId,t.BusinessDistrictName";
		}
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, BusinessDistrict.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getBusinessDistrictTotal(PCPSWebInput webInput) {
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			String sql = "select nvl(count(*),0) from BusinessDistrict t where t.IssueCompanyNo = ?1";
			Query query = _em.createNativeQuery(sql);
			query.setParameter(1, issueCompanyNo);
			return new BigInteger(query.getSingleResult().toString());
		} else {
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from BusinessDistrict t");
			return new BigInteger(query.getSingleResult().toString());
		}
	}

	@Override
	public void insertBusinessDistrict(BusinessDistrict businessDistrict)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, businessDistrict);
	}

	@Override
	public void updateBusinessDistrict(BusinessDistrict businessDistrict)
			throws PCPSApplicationException {
		GeneralDao.update(_em, businessDistrict);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessDistrict> getBusinessDistrictDropdownList(
			PCPSWebInput webInput) {
		String sql = "";
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select * from BusinessDistrict c where c.IssueCompanyNo = '"
					+ issueCompanyNo + "' order by c.businessdistrictid";
		} else {
			sql = "select * from BusinessDistrict c order by c.businessdistrictid";
		}
		return _em.createNativeQuery(sql, BusinessDistrict.class)
				.getResultList();
	}

	/**
	 * 验证商圈名称重复
	 * 
	 * @param issuecompanyno
	 */
	public Integer IsBusinessDistrictName(String issueCompanyNo,
			String businessDistrictName, BigInteger businessDistrictId) {
		if (PCPSUtil.isEmpty(businessDistrictName)
				|| PCPSUtil.isEmpty(issueCompanyNo))
			return 1;
		String sql = "select nvl(count(*),0) from BusinessDistrict where IssueCompanyNo=?1 and businessDistrictName=?2 and (businessDistrictId != ?3 or ?3 is NULL)";
		// if(businessDistrictId!=null)
		// sql=sql+" and businessDistrictId!="+businessDistrictId+"";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, businessDistrictName);
		query.setParameter(3, businessDistrictId);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	/**
	 * 根据给定的商圈编号获取自身及下级子商圈列表
	 * 
	 * @param businessDistrictId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<BusinessDistrict> getSelfAllChildBusinessDistrictByBusinessDistrictId(
			BigInteger businessDistrictId) {
		String sql = "select t.* from BusinessDistrict t ";
		sql += " start with t.businessdistrictid = ?1 ";
		sql += " CONNECT BY PRIOR t.businessdistrictid = t.superbusinessdistrictid";
		Query query = _em.createNativeQuery(sql, BusinessDistrict.class);
		query.setParameter(1, businessDistrictId);
		return query.getResultList();
	}

	@Override
	public void deleteBusinessDistrict(BigInteger businessDistrictId)
			throws PCPSApplicationException {
		String sql = "delete from BusinessDistrict t where t.businessDistrictId = ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessDistrictId);
		try {
			query.executeUpdate();
		} catch (Exception e) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "删除商圈失败,已经被引用!");
		}
	}

	/**
	 * 检查某商圈是否已经被引用(商圈绑定商户BusinessDistrictMerchant)和(卡种类绑定商圈CardClassicBindDistrict)
	 * 
	 * @return
	 */
	@Override
	public Integer checkBusinessDistrictMerchant(BigInteger businessDistrictId) {
		// 商圈绑定商户
		String sql = "select nvl(count(*),0) from BusinessDistrictMerchant t"
				+ " where t.businessDistrictId = ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessDistrictId);
		Integer count = new Integer(query.getSingleResult().toString());
		return count;
	}

	/**
	 * 检查某商圈是否已经被引用(卡种类绑定商圈CardClassicBindDistrict)
	 * 
	 * @param businessDistrictId
	 * @return
	 */
	public Integer checkBusinessDistrictCardClassic(
			BigInteger businessDistrictId) {
		// 卡种类绑定商圈
		String sql = "select nvl(count(*),0) from CardClassicBindDistrict t"
				+ " where t.businessDistrictId  = ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessDistrictId);
		Integer count = new Integer(query.getSingleResult().toString());
		return count;
	}

	/**
	 * 检查某商圈是否已经被引用(二级商圈)
	 * 
	 * @param businessDistrictId
	 * @return
	 */
	public Integer checkBusinessDistrictChild(BigInteger businessDistrictId) {
		// 下级商圈
		String sql = "select nvl(count(*),0) from businessdistrict t"
				+ " where t.superbusinessdistrictid = ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, businessDistrictId);
		Integer count = new Integer(query.getSingleResult().toString());
		return count;
	}
}
